package com.ag.hmk.action;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import com.ag.bean.TbOverRequest;
import com.ag.bean.TbUser;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IPermissionService;
import com.ag.common.PageUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
import com.ag.lzw.action.TaskTrackAction;
import com.ag.lzw.dao.LzwTbEventDAO;
import com.ag.lzw.service.HandelTasks;
import com.ag.zrt.dao.TbProposeDAO;

/**
 * 
 * @author hmk 查询任务办结
 */
public class FindTaskOrAction extends BaseAction implements SessionAware {
	private static final long serialVersionUID = 1883171960655616213L;
	private static final Logger logger = Logger
			.getLogger(TaskTrackAction.class);

	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;

	@Resource(name = "handelTaskImpl")
	private HandelTasks handelTask;

	@Resource(name = "lzwTbEventDAO")
	private LzwTbEventDAO tbEventDAO;

	@Resource(name = "ZRTTbProposeDAO")
	private TbProposeDAO proposeDAO;

	private HmkUiltServImpl hmkUiltServImpl;
	private int page;
	private String tID;

	private PageUtil pageUtil; // 返回结果

	/*
	 * 查询任务办结(by ID)
	 */
	public String findTOR() throws Exception {
		TbUser user = pms.getCurrentUser(sessionMap); // 获取当前用户信息
		String cUname = user.getUName();
		String uid = user.getUId(); // 获取当前用户ID
		logger.info("***************Current userID:" + uid);

		PageUtil pUtil = new PageUtil();
		List<TbOverRequest> result = null;

		int total = 0;
		int j = 0;
		int newtotal = 0;
		try {
			System.out.println("+++++++++tid+++" + tID);
			String hql = "from TbOverRequest tor left join fetch tor.tbOverStatus left join fetch tor.tbTask tt left join fetch "
					+ " tt.tbTaskStatus  where tt.TId ='"
					+ tID
					+ "'"
					+ " order by tor.toTime desc";
			if (page == 0) {
				page = 1;
			}
			int firstRow = (page - 1) * 10;
			result = hmkUiltServImpl.query(hql, firstRow, 10);
			if (result != null) {
				total = result.size();
				System.out.println("+++++++++total first+++" + total);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
			total = 0;
		}
		System.out.println("+++++++++total first+++" + total);
		try {
			System.out.println("+++++++++total first+++" + total);
			System.out.println("UID***************************"+getSession().getAttribute(EventUtil.PROCESSING_EVENT_ID));
			String UId = this.proposeDAO.findProposeByEventId(handelTask.queryTaskById(tID).getEId()).getTbUserByPViceCommander().getUId();
			System.out.println("UID***************************"+UId);
			if (UId.equals(uid)|| ((handelTask.queryTaskById(handelTask.queryTaskById(tID)
							.getFid()) != null) && cUname.equals(handelTask
							.queryTaskById(
									handelTask.queryTaskById(tID).getFid())
							.getUname()))) {
//				System.out.println("++++++euid+++++++"
//						+ this.tbEventDAO.findById(
//								handelTask.queryTaskById(tID).getEId())
//								.getTbUserByTbUId().getUId());
				List<TbOverRequest> newresult = new LinkedList<TbOverRequest>();
				try {
					for (int i = 0; i < result.size(); i++) {
						System.out.println("++++==+++++getSname+++"
								+ result.get(i).getTbOverStatus().getSname());
						if (!result.get(i).getTbOverStatus().getSname().equals(
								"办结收回")) {
							System.out.println("++++==+++++getSname  af+++"
									+ result.get(i).getTbOverStatus()
											.getSname());
							newresult.add(result.get(i));
							newtotal++;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("+++++==++++total after+++"
						+ newresult.size());
				pUtil.setTotal(newtotal);
				pUtil.setPage(page);
				pUtil.setMaxRow(10);
				pUtil.setList(newresult);
				this.setPageUtil(pUtil);
				return "findOverReqOK";
			} else {
				pUtil.setTotal(total);
				pUtil.setPage(page);
				pUtil.setMaxRow(10);
				pUtil.setList(result);
				this.setPageUtil(pUtil);
				return "findOverReqOK";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "findOverReqOK";
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getTID() {
		return tID;
	}

	public void setTID(String tID) {
		this.tID = tID;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;

	}

}
